'''
Created on 2011-9-1

@author: Administrator
'''
import Queue
import db.operator

class Manager():
    def __init__(self):
        self.request = Queue.Queue()
        self.respond = Queue.Queue()
        self.operator = db.operator.Opertor(self.request, self.respond)
        self.handlerlist = {}
        self.exits = False
        
    def Init(self, url):
        self.operator.Init(url)
    
    def Uninit(self):
        self.operator.Uninit()
    
    def PutRequest(self, request):
        self.request.put(request)
    
    def RegisterProtocol(self, id, fun):
        if id in self.handlerlist:
            raise ValueError('id {0} has in list'.format(id))
        self.handlerlist[id] = fun
        
    
    def UnregisterProtocol(self, id):
        if id in self.handlerlist:
            self.handlerlist.pop(id)
            
    def Loop(self):
        while not self.respond.empty():
            respond = self.respond.get()
            self.handlerlist[respond.id](respond)
    
    
    def LoadAllCaseTask(self):
        return self.operator.LoadAllCaseTask()
    
    def LoadAllMachine(self):
        return self.operator.LoadAllMachine()
    
    def LoadOSList(self):
        return self.operator.LoadOSList()
    
    # TaskFlow
    def LoadAllTaskFlow(self):
        return self.operator.LoadAllTaskFlow()
    
    def Start(self):
        self.operator.Start()